package com.situ.web.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.situ.web.dao.IStudentDao;
import com.situ.web.entity.Banji;
import com.situ.web.entity.Student;
import com.situ.web.util.JDBCUtil;
import com.situ.web.util.ModelConvertUtil;

public class StudentDaoMySqlImpl implements IStudentDao{
	
	@Override
	public List<Student> selectAll() {
		ArrayList<Student> list = new ArrayList<Student>();
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,`name`,age,gender,banji_id FROM student";
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				int banjiId = resultSet.getInt("banji_id");
				Student student = new Student(id, name, age, gender,banjiId);
				list.add(student);
			}
			
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		return list;
	}

	@Override
	public List<Student> search(String searchName) {
		
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<Student> list = new ArrayList<Student>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "Select id,`name`,age,gender from student Where name like ? ";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, "%"+searchName+"%");
			
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return list;
	}

	@Override
	public Integer insert(Student student) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO student(`name`,age,gender,banji_id) VALUES(?,?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, student.getName());
			preparedStatement.setInt(2, student.getAge());
			preparedStatement.setString(3, student.getGender());
			preparedStatement.setInt(4, student.getBanjiId());
			count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("插入成功");
			} else {
				System.out.println("插入失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return count;
	}

	@Override
	public Integer update(Student student) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "update student set name = ?,age = ?,gender = ?,banji_id = ? where id = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, student.getName());
			preparedStatement.setInt(2, student.getAge());
			preparedStatement.setString(3, student.getGender());
			preparedStatement.setInt(4, student.getBanjiId());
			preparedStatement.setInt(5, student.getId());
			count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("更改成功");
			} else {
				System.out.println("更改失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		return count;
	}

	@Override
	public Integer deleteById(Integer id) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count =0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "delete from student where id = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			 count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("删除成功");
			}else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		return count;
	}

	@Override
	public List<Student> selectByName(String searchName) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<Student> list = new ArrayList<Student>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "Select id,`name`,age,gender from student Where name = ? ";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, searchName);
			
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return list;
	}

	@Override
	public List<Student> selectById(Integer iD) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<Student> list = new ArrayList<Student>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "Select id,`name`,age,gender,banji_id from student Where id =? ";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1,iD);
			
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				int banjiId = resultSet.getInt("banji_id");
				Student student = new Student(id, name, age, gender,banjiId);
				list.add(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return list;
	}

	@Override
	public Student selectById2(Integer iD) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		Student student = new Student();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "Select id,`name`,age,gender from student Where id =? ";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, iD);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				student = new Student(id, name, age, gender);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return student;
	}

	@Override
	public Integer getTotalCount() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT COUNT(*) FROM student";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			if (resultSet.next()) {
				count = resultSet.getInt("count(*)");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		System.out.println(count);
		return count;
	}

	@Override
	public List<Student> pageList(int offset, Integer pageSize) {
		List<Student> list = new ArrayList<Student>();
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT * FROM student LIMIT ?,?";
			System.out.println(sql);
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, offset);
			preparedStatement.setInt(2, pageSize);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				int banjiId = resultSet.getInt("banji_id");
				Student student = new Student(id, name, age, gender,banjiId);
				list.add(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return list;
	}

	@Override
	public ArrayList<Banji> selectBanjiAll() {
		ArrayList<Banji> list = new ArrayList<Banji>();
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,`name` FROM banji";
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				Banji banji = new Banji(id, name);
				list.add(banji);
			}
			
			for (Banji banji : list) {
				System.out.println(banji);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> selectStudentBanjiInfo() {
		List<Map<String, Object>> list = null;
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT s.id,s.name,s.age,s.gender,b.name AS banjiName FROM student AS s INNER JOIN banji AS b ON s.banji_id = b.id;";
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			list = ModelConvertUtil.converToList(resultSet);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		return list;
	}

}
